using UnityEngine;
using System;
using System.Collections;

public class FightingUI : MonoBehaviour {
	
	public GUISkin mainSkin;
	
	private AlienFighting playerRoll;
	private AlienFighting enemyRoll;

	public GameObject playerFightStatsObj;
	public GameObject enemyFightStatsObj;
	
	public AlienStatsUI player;
	public AlienStatsUI enemy;
	
	public AlienDialog playerDialog;
	public AlienDialog enemyDialog;
	public AlienDialog multiplier;
	
	public AlienScore playerScore;
	public AlienScore enemyScore;

	public delegate void ButtonEvent ();
	public ButtonEvent done;
	public ButtonEvent finished;
	

	// Use this for initialization
	public bool testing= true;
	void Start () {
		//TESTING
		//TESTING
		if(testing) {
			GameObject player= AlienMating.spawnAlien(1);
			StartFight(player.GetComponent<AlienGenome>(), 1);
			Destroy(player);
		}
		//END TESTING
		//END TESTING
	}
	
	public void StartFight(AlienGenome playerAlien, int enemyDifficulty) {
		GameObject playerObj= AlienMating.duplicateAlien(playerAlien);
		player.attachAlien(playerObj.GetComponent<AlienGenome>());
		
		GameObject enemyObj= AlienMating.spawnAlien(enemyDifficulty);
		enemy.attachAlien(enemyObj.GetComponent<AlienGenome>());
		
		gotoStart();
	}
	
	public void StartFight(AlienGenome playerAlien, AlienGenome enemyAlien) {
		GameObject playerObj= AlienMating.duplicateAlien(playerAlien);
		player.attachAlien(playerObj.GetComponent<AlienGenome>());
		
		GameObject enemyObj= AlienMating.duplicateAlien(enemyAlien);
		enemy.attachAlien(enemyObj.GetComponent<AlienGenome>());
		
		
		gotoStart();		
	}
		
	private enum STATE { 
		START, 
		FIGHT, 
		TIE,
		WAIT, 
		END };
	private STATE state= STATE.START;
	private Chromosome.ChromosomeType currentChromosome;
	// Update is called once per frame
	void Update () {
	}
	
	void OnGUI() {
		GUI.skin= mainSkin;
		if(state == STATE.START || state == STATE.WAIT) {
			Vector2 pos= new Vector2( Screen.width * 0.33f, Screen.height * 0.75f);
			if(GUI.Button(new Rect( pos.x, pos.y, Screen.width * 0.33f, Screen.height * 0.15f), "F I G H T!"))
			{
				Debug.Log ("Goto Fight");
				gotoFight();
			}				
		}
		
		if((int)state > (int)STATE.START) {
			//Debug.Log ("Show Results");
			drawFightRoll(playerFightStatsObj, playerRoll);
			drawFightRoll(enemyFightStatsObj, enemyRoll);	
		}
		
		if(state == STATE.TIE) {
			{
				Vector2 pos= new Vector2( Screen.width * 0.05f, Screen.height * 0.75f);
				if(GUI.Button(new Rect( pos.x, pos.y, Screen.width * 0.4f, Screen.height * 0.15f), "Double Down !"))
				{
					gotoFight(true);
				}
			}
			{
				Vector2 pos= new Vector2( Screen.width * 0.55f, Screen.height * 0.75f);
				if(GUI.Button(new Rect( pos.x, pos.y, Screen.width * 0.4f, Screen.height * 0.15f), "Accept the Tie ."))
				{
					gotoWait();
				}
			}
		}
		if( state == STATE.END) {
			Vector2 screenCoord= new Vector2(0.6f, 0.15f);
			if(GUI.Button( new Rect(screenCoord.x * (float)Screen.width, (1-screenCoord.y)* (float)Screen.height, Screen.width *0.40f, Screen.height *0.15f), "S c i e n c e !")){
				if(done != null)
					done();
			}		
		}
		
	}
					
	private void drawFightRoll(GameObject anchor, AlienFighting fightRoll) {
		Vector3 worldPoint= anchor.transform.position;
		Vector2 screenCoord= Camera.mainCamera.WorldToViewportPoint (worldPoint);

		string output="";
		for(int type=0; type< (int)currentChromosome+1; type++)
		{
			output+= ((Chromosome.ChromosomeType)type).ToString() + "=> " + fightRoll.Roll[(Chromosome.ChromosomeType)type] + "\n";
		}
		GUI.Label(new Rect(screenCoord.x * (float)Screen.width, (1-screenCoord.y)* (float)Screen.height, 150, 200), output);
	
	}
	
	
	void gotoStart() {
		state= STATE.START;
		doubleDownMult= 1;
		releaseFinalStage();
		multiplier.text= doubleDownMult.ToString() + "x";
		playerScore.score=0;
		enemyScore.score=0;
		playerDialog.text="";
		enemyDialog.text="";
		
		player.rotate= false;
		enemy.rotate= false;
	}
	
	
	private int doubleDownMult=1;
	void gotoFight(bool repeat= false) {
		if(state == STATE.START) {
			playerRoll= new AlienFighting();
			enemyRoll=  new AlienFighting();
			doubleDownMult= 1;
			multiplier.text= doubleDownMult.ToString() + "x";
			currentChromosome= (Chromosome.ChromosomeType)(0);
		}
		else if (repeat) {
			//doubleDownMult*=2;
			doubleDownMult++;
			multiplier.text= doubleDownMult.ToString() + "x";
		}
		else {//Next chromosome
			doubleDownMult= 1;
			multiplier.text= doubleDownMult.ToString() + "x";
			currentChromosome= (Chromosome.ChromosomeType)( (int)currentChromosome + 1);
		}
		
		
		state= STATE.FIGHT;	
		playerRoll.rollChromosome(player.target.getChromosome( currentChromosome ) );
		enemyRoll.rollChromosome(enemy.target.getChromosome( currentChromosome ) );
		
		
		if(playerRoll.Roll[currentChromosome] > enemyRoll.Roll[currentChromosome] ) {
			playerDialog.text= "Win!";	
			enemyDialog.text= "Lose!";
			playerDialog.font= "Win";
			enemyDialog.font= "Lose";
			
			//playerScore.score+= doubleDownMult * (playerRoll.Roll[currentChromosome] - enemyRoll.Roll[currentChromosome]);
			playerScore.score+= doubleDownMult;
			gotoWait();
		}
		else if(playerRoll.Roll[currentChromosome] < enemyRoll.Roll[currentChromosome] ) {
			enemyDialog.text= "Win!";	
			playerDialog.text= "Lose!";
			gotoWait();
			
			enemyDialog.font= "Win";
			playerDialog.font= "Lose";
			
			
			//enemyScore.score+= doubleDownMult * (enemyRoll.Roll[currentChromosome] - playerRoll.Roll[currentChromosome]);
			enemyScore.score+= doubleDownMult;
		}
		else {
			enemyDialog.text= "Tie!";	
			playerDialog.text= "Tie!";
			playerDialog.font= "Tie";
			enemyDialog.font= "Tie";
			
			gotoTie();
		}
		
		
	}
	
	void gotoTie() {
		state= STATE.TIE;
		multiplier.text= doubleDownMult.ToString() + "x";
	}
	
	void gotoWait() {
		state= STATE.WAIT;
		Debug.Log("CurrentChromosome: " + (int)currentChromosome + " out of " + (Enum.GetNames(typeof(Chromosome.ChromosomeType)).Length-1));
		if((int)currentChromosome == Enum.GetNames(typeof(Chromosome.ChromosomeType)).Length-1) {
			gotoEnd();
		}
	}
	
	void gotoEnd() {
		Debug.Log("Go to End");
		state= STATE.END;
		
		if(enemyScore.score > playerScore.score) {
			playerDialog.text= "Total Loser!";	
			enemyDialog.text= "Ultimate Victor!";
			setFinalStage(enemy, player);
			enemyDialog.font= "GrandWin";
			playerDialog.font= "GrandLose";
		}
		else if(enemyScore.score < playerScore.score) {
			enemyDialog.text= "Total Loser!";	
			playerDialog.text= "Ultimate Victor!";
			setFinalStage(player, enemy);
			playerDialog.font= "GrandWin";
			enemyDialog.font= "GrandLose";
		}
		else {
			enemyDialog.text= "Absolute Tie! Not a Winner!";	
			playerDialog.text= "Absolute Tie! Also Not a Winner!";
			playerDialog.font= "Tie";
			enemyDialog.font= "Tie";
			releaseFinalStage();
		}
		
		if(finished != null)
			finished();
	}
	
	void setFinalStage(AlienStatsUI winner, AlienStatsUI loser) {
		winner.transform.localScale= new Vector3(3.5f, 3.5f, 3.5f);
		winner.rotate= true;
		loser.transform.localScale= new Vector3(1,1,1);
	}
	
	public void releaseFinalStage() {
		player.rotate=false;
		enemy.rotate=false;
		player.transform.localScale= new Vector3(3,3,3);
		enemy.transform.localScale= new Vector3(3,3,3);
		//player.ClearGore();
		//enemy.ClearGore();
	}
}
